package com.minshenglife.zanzan.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.RedFlowerDao;
import com.minshenglife.zanzan.entity.RedFlower;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.result.RedFlowerResult;
import com.minshenglife.zanzan.result.UserResult;
import com.minshenglife.zanzan.service.RedFlowerService;


/**
 * 红花流水service
 * @author huodd
 *
 */
@Component
@Transactional
public class RedFlowerServiceImpl implements RedFlowerService{

	@Resource
	RedFlowerDao flowerDao;
	
	@Override
	public void add(RedFlower redFlower) {
		redFlower.setState(Keys.REDFLOWER_STATE_ONE);
		flowerDao.add(redFlower);
	}
	

	@Override
	public Page<UserResult> findProgressRank(Map<String, Object> map,
			int pageNo, int pageSize) {
		if(map==null)
			map=new HashMap<String,Object>();
		int offset = (pageNo-1) * pageSize;
        map.put("from", offset);
        map.put("to", offset+pageSize);
        //设置查询的时间范围
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
        map.put("today", sdf.format(c.getTime()));
        c.add(Calendar.DAY_OF_MONTH, -1);
        map.put("beforeDay", sdf.format(c.getTime()));
        
        Long count =flowerDao.countFlowerProgressBank(map);
        List<UserResult> rows =flowerDao.findFlowerProgressBank(map);
        Page<UserResult> ret = new Page<UserResult>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
        return ret;
	}


	@Override
	public List<RedFlower> findMyRedFlowerList(String empId,Date startDate,Date endDate,boolean isHasRule) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("sendManId", empId);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		//是规则活动期间
		if(isHasRule){
			 sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			 map.put("isHasRule", "isHasRule");
		}else{
			map.put("isNotHasRule", "isNotHasRule");
		}
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		List<RedFlower> list=flowerDao.findBy(map);
		return list;
	}


	@Override
	public Long countRedFlowerOfMonth(String fromEmpId,String toEmpId,Date startDate,Date endDate){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("sendManId", fromEmpId);
		map.put("addManId", toEmpId);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("isNotHasRule", "isNotHasRule");
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		return flowerDao.count(map);
	}

	@Override
	public Long countRedFlowerOfMonthOfDept(String fromEmpId,String toEmpId,Date startDate,Date endDate, boolean isHasRule){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("sendManId", fromEmpId);
		map.put("addManId", toEmpId);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		//是规则活动期间
		if(isHasRule){
			 sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			 map.put("isHasRule", "isHasRule");
		}else{
			map.put("isNotHasRule", "isNotHasRule");
		}
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		map.put("monthOfDept", "monthOfDept");
		return flowerDao.count(map);
	}
	
	public static void main(String[] asd){
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	        Calendar c=Calendar.getInstance();
	       	System.out.println(sdf.format(c.getTime()));
	        c.add(Calendar.DAY_OF_MONTH, -1);
	    	System.out.println(sdf.format(c.getTime()));
	}

	@Override
	public Page<RedFlowerResult> findMySendFlowerList(String sendManId,
			int pageNo, int pageSize) {
		Map<String,Object> map=new HashMap<String,Object>();
        map.put("sendManId", sendManId);
        
        Page<RedFlowerResult> ret = redFlowerList(pageNo, pageSize, map);
        return ret;
	}

	@Override
	public Page<RedFlowerResult> findSendMyFlowerList(String addManId,
			int pageNo, int pageSize) {
		Map<String,Object> map=new HashMap<String,Object>();
        map.put("addManId", addManId);
        
        Page<RedFlowerResult> ret = redFlowerList(pageNo, pageSize, map);
        return ret;
	}

	/**
	 * 获取红花记录列表
	 * @param pageNo
	 * @param pageSize
	 * @param map
	 * @return
	 */
	private Page<RedFlowerResult> redFlowerList(int pageNo, int pageSize,
			Map<String, Object> map) {
		int offset = (pageNo-1) * pageSize;
        map.put("from", offset);
        map.put("to", offset+pageSize);
        Long count =flowerDao.countRedFlowerList(map);
        List<RedFlowerResult> rows =flowerDao.findRedFlowerList(map);
        Page<RedFlowerResult> ret = new Page<RedFlowerResult>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
		return ret;
	}


	@Override
	public Page<RedFlowerResult> findYesterdayFlowerRank(
			Map<String, Object> map, int pageNo, int pageSize) {
		if(map==null)
			map=new HashMap<String,Object>();
        int offset = (pageNo-1) * pageSize;
        map.put("from", offset);
        map.put("to", offset+pageSize);
        //设置查询的时间范围
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
//        map.put("endDate", sdf.format(c.getTime()));
      //如果是周一，则查询上周五的红花信息
        if(c.get(Calendar.DAY_OF_WEEK)==2){
        	c.add(Calendar.DAY_OF_WEEK, -3);
        }else{
        	c.add(Calendar.DAY_OF_MONTH, -1);
        }
        map.put("startDate", sdf.format(c.getTime()));
        map.put("endDate", sdf.format(c.getTime()));
        
        Long count =flowerDao.countRedFlowerRank(map);
        List<RedFlowerResult> rows =flowerDao.findRedFlowerRank(map);
        Page<RedFlowerResult> ret = new Page<RedFlowerResult>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
		return ret;
	}


	@Override
	public Page<RedFlowerResult> findYesterdayFlowerListByCon(String content, int pageNo, int pageSize) {
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("content", content);
		int offset = (pageNo-1) * pageSize;
        map.put("from", offset);
        map.put("to", offset+pageSize);
        //设置查询的时间范围
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
//        map.put("endDate", sdf.format(c.getTime()));
        //如果是周一，则查询上周五的红花信息
        if(c.get(Calendar.DAY_OF_WEEK)==2){
        	c.add(Calendar.DAY_OF_WEEK, -3);
        }else{
        	c.add(Calendar.DAY_OF_MONTH, -1);
        }
        map.put("startDate", sdf.format(c.getTime()));
        map.put("endDate", sdf.format(c.getTime()));
        Long count =flowerDao.countYesterdayFlowerListByCon(map);
		List<RedFlowerResult> rows =flowerDao.findYesterdayFlowerListByCon(map);
        Page<RedFlowerResult> ret = new Page<RedFlowerResult>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
		return ret;
	}

	@Override
	public Long getAddRedFlowerCountOfTerm(String empId, Date startDate,
			Date endDate) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("addManId", empId);
		map.put("state", Keys.REDFLOWER_STATE_ZERO);
		map.put("notEqSendManId", Keys.SYSTEM_SUPER_USER);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		Long num=flowerDao.redFlowerTotalNum(map);
		return num;
	}

	@Override
	public Page<RedFlowerResult> getAddRedFlowerInfoOfTerm(String empId,
			Date startDate, Date endDate, int pageNo, int pageSize) {
		Map<String,Object> map=new HashMap<String,Object>();
		int offset = (pageNo-1) * pageSize;
        map.put("from", offset);
        map.put("to", offset+pageSize);
		map.put("addManId", empId);
		map.put("state", Keys.REDFLOWER_STATE_ZERO);
//		map.put("notEqSendManId", Keys.SYSTEM_SUPER_USER);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		Long count=flowerDao.countRedFlowerList(map);
		List<RedFlowerResult> rows =flowerDao.findRedFlowerList(map);
        Page<RedFlowerResult> ret = new Page<RedFlowerResult>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
		return ret;
	}


	@Override
	public boolean isEqDept(String sendManId, String addManId) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("sendManId", sendManId);
		map.put("addManId", addManId);
		int val=flowerDao.isEqDept(map);
		return val>=1 ? true : false ;
	}
	
}
